package com.wcs.spring.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wcs.spring.entity.ClassInfo;
import com.wcs.spring.entity.ClassTable;
import com.wcs.spring.entity.OrderClass;
import com.wcs.spring.entity.User;
import com.wcs.spring.service.ClassInfoService;
import com.wcs.spring.service.ClassTableService;
import com.wcs.spring.service.UserService;
import com.wcs.spring.util.Const;
import com.wcs.spring.util.PageUtil;
import com.wcs.spring.util.WeekDateUtil;

@Controller
@RequestMapping(value = "mgt/classTable")
public class ClassTableController {
	@Resource(name = "classTableService")
	private ClassTableService classTableService;

	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "classInfoService")
	private ClassInfoService classInfoService;

	@RequestMapping(value = "classTableList")
	public ModelAndView classTableList(ClassTable classTable, PageUtil pageUtil, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ClassTable> list = this.classTableService.queryClassTableList(classTable, pageUtil);

		map.put("classTableList", list);
		return new ModelAndView("mgt/classTable/classTable_list", map);
	}

	@RequestMapping(value = "orderClassList")
	public ModelAndView orderClassList(OrderClass orderClass, PageUtil pageUtil, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		orderClass.setSchoolId(sessionUser.getSchoolId());
		Map<String, Object> map = new HashMap<String, Object>();
		PageInfo<User> pager = null;
		try {
			PageHelper.startPage(pageUtil.getPageNum(), pageUtil.getPageSize());
			List<ClassTable> list = this.classTableService.queryOrderClassList(orderClass, null, null);
			pager = new PageInfo(list);
		}
		catch (Exception e) {
			System.out.println("error:" + e.getMessage());
		}
		map.put("pager", pager);
		map.put("pageUtil", pageUtil);

		return new ModelAndView("mgt/classTable/orderClass_list", map);
	}

	@RequestMapping(value = "toClassTableUpdate")
	public @ResponseBody
	ClassTable toClassTableUpdate(Integer id, HttpServletResponse response) throws Exception {
		ClassTable classTable = classTableService.queryClassTableById(id);
		return classTable;
	}

	@RequestMapping(value = "toClassTableShow")
	public @ResponseBody
	ClassTable toClassTableShow(Integer id, HttpServletResponse response) throws Exception {
		ClassTable classTable = classTableService.queryClassTableById(id);
		OrderClass orderClass = new OrderClass();
		orderClass.setClassTableId(id);
		String studentNames = "";
		List<OrderClass> orderClassList = classTableService.queryOrderClassList(orderClass);
		Iterator<OrderClass> it = orderClassList.iterator();
		while (it.hasNext()) {
			OrderClass oc = it.next();
			studentNames += oc.getStudentName() + "  ";
		}
		classTable.setStudentNames(studentNames);
		return classTable;
	}

	@RequestMapping(value = "classTableSave")
	public @ResponseBody
	ClassTable classTableSave(ClassTable classTable, HttpServletRequest request) throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		Date date = new Date();
		classTable.setCreateTime(date);
		classTable.setUpdateTime(date);
		classTable.setCreateUser(sessionUser.getUserName());
		classTable.setUpdateUser(sessionUser.getUserName());
		classTable.setSchoolId(sessionUser.getSchoolId());
		if ("all".equals(classTable.getLevelType())) {
			classTable.setClassLevel(",0,1,2,3,4,5,6,7,8,9,");
		}
		else if ("above".equals(classTable.getLevelType())) {
			classTable.setClassLevel(getClassLevelStr(Integer.parseInt(classTable.getClassLevel()), 9));
		}
		else if ("equals".equals(classTable.getLevelType())) {
			classTable.setClassLevel("," + classTable.getClassLevel() + ",");
		}
		else {
			classTable.setClassLevel(getClassLevelStr(Integer.parseInt(classTable.getLevelType()),
					Integer.parseInt(classTable.getClassLevel())));
			classTable.setLevelType("interval");
		}
		int id = classTableService.addClassTable(classTable);
		ClassTable classTable2 = classTableService.queryClassTableById(classTable.getId());
		return classTable2;
	}

	private String getClassLevelStr(int start, int end) {
		String classLevel = ",";
		for (int i = start; i < end + 1; i++) {
			classLevel = classLevel + i + ",";
		}
		return classLevel;
	}

	@RequestMapping(value = "classTableUpdate")
	public @ResponseBody
	ClassTable classTableUpdate(ClassTable classTable, HttpServletRequest request) throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		classTable.setUpdateTime(new Date());
		classTable.setUpdateUser(sessionUser.getUserName());
		if ("all".equals(classTable.getLevelType())) {
			classTable.setClassLevel(",0,1,2,3,4,5,6,7,8,9,");
		}
		else if ("above".equals(classTable.getLevelType())) {
			classTable.setClassLevel(getClassLevelStr(Integer.parseInt(classTable.getClassLevel()), 9));
		}
		else if ("equals".equals(classTable.getLevelType())) {
			classTable.setClassLevel("," + classTable.getClassLevel() + ",");
		}
		else {
			classTable.setLevelType("interval");
			classTable.setClassLevel(getClassLevelStr(Integer.parseInt(classTable.getLevelType()),
					Integer.parseInt(classTable.getClassLevel())));
		}
		classTableService.updateClassTable(classTable);
		ClassTable classTable2 = classTableService.queryClassTableById(classTable.getId());
		return classTable2;
	}

	@RequestMapping(value = "classTableDelete")
	public @ResponseBody
	ClassTable classTableDelete(ClassTable classTable, HttpServletResponse response) throws Exception {
		classTableService.delClassTable(classTable);
		return classTable;
	}

	@RequestMapping(value = "nowWeekClass")
	public ModelAndView nowWeekClass(ClassTable classTable, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> weekDateList = WeekDateUtil.getWeekdays(null);
		map.put("weekDateList", weekDateList);

		return new ModelAndView("mgt/classTable/nowWeekClass", map);
	}

	@RequestMapping(value = "queryClassTableList")
	public @ResponseBody
	Map<String, Object> queryClassTableList(ClassTable classTable, HttpServletRequest request) throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> weekDateList = WeekDateUtil.getWeekdays(classTable.getClassDate());
		String beginDate = weekDateList.get(0);
		String endDate = weekDateList.get(6);
		classTable.setSchoolId(sessionUser.getSchoolId());
		List<ClassTable> classTableList = this.classTableService.queryClassTableList(classTable, beginDate, endDate);
		map.put("weekDateList", weekDateList);
		map.put("classTableList", classTableList);
		return map;
	}

	@RequestMapping(value = "otherWeekClass")
	public ModelAndView otherWeekClass(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/classTable/otherWeekClass");
	}

	@RequestMapping(value = "queryClassAndTeacher")
	public @ResponseBody
	Map<String, Object> queryClassAndTeacher(HttpServletRequest request) throws Exception {
		User sessionUser = (User) request.getSession().getAttribute(Const.SESSION_SYS_USER);
		Map<String, Object> map = new HashMap<String, Object>();
		User user = new User();
		user.setUserType("js");
		ClassInfo classInfo = new ClassInfo();
		classInfo.setSchoolId(sessionUser.getSchoolId());
		List<ClassInfo> classInfoList = classInfoService.queryClassInfoSimple(classInfo);
		List<User> userList = userService.queryUserByType(user);
		map.put("classInfoList", classInfoList);
		map.put("userList", userList);
		return map;
	}

}
